package game.entity;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;

public class AbsAnimation {
	protected int id;
	Animation                       animation; 
    Texture                         texture;     
    TextureRegion[]                 frames; 
    TextureRegion                   curFrame;
    protected float timer = 0;
    protected float frameDuration;
	public AbsAnimation(int idAnimation, String imgPath, float frameDuration, int frame_rows, int frame_cols)
	{
		texture = new Texture(Gdx.files.internal(imgPath));    
        TextureRegion[][] tmp = TextureRegion.split(texture, texture.getWidth() / frame_cols, 
        											texture.getHeight() / frame_rows);                
        frames = new TextureRegion[frame_cols * frame_rows];
        int index = 0;
        for (int i = 0; i < frame_rows; i++) {
                for (int j = 0; j < frame_cols; j++) {
                        frames[index++] = tmp[i][j];
                }
        }
        id = idAnimation;
        this.frameDuration = frameDuration;
        animation = new Animation(frameDuration, frames);    
	}
	
	public AbsAnimation(AbsAnimation absAnimation)
	{
		this.id = absAnimation.id;
	    this.texture = absAnimation.texture;     
	    curFrame = absAnimation.curFrame;
	    this.timer = 0;
	    this.frameDuration = absAnimation.frameDuration;
	    
	    int nframes = absAnimation.frames.length; 
	    this.frames = new TextureRegion[nframes];
	    for(int i=0; i<nframes;i++)
	    	this.frames[i] = absAnimation.frames[i]; 
	    
		this.animation = new Animation(this.frameDuration,frames); 
	}
	
	public AbsAnimation(int idAnimation, String imgPath, float frameDuration ,int scrX, int scrY, int frameWidth, int frameHeight, int frame_rows, int frame_cols)
	{
		texture = new Texture(Gdx.files.internal(imgPath));
		init(idAnimation,frameDuration, scrX, scrY, frameWidth, frameHeight, frame_rows, frame_cols);    
	}

	public AbsAnimation(int idAnimation, Texture texture, float frameDuration ,int scrX, int scrY, int frameWidth, int frameHeight, int frame_rows, int frame_cols)
	{
		this.texture = texture;
		init(idAnimation,frameDuration, scrX, scrY, frameWidth, frameHeight, frame_rows, frame_cols);   
	}

	private void init(int idAnimation, float frameDuration, int scrX, int scrY, int frameWidth, int frameHeight, int frame_rows, int frame_cols) 
	{
		this.id = idAnimation;
		this.frameDuration = frameDuration;
		frames = new TextureRegion[frame_cols * frame_rows];
		for(int i=0; i<frame_rows; i++)
			for(int j=0; j<frame_cols; j++)
			{
				frames[i*frame_cols + j] = new TextureRegion(texture, scrX + j*frameWidth, scrY +i*frameHeight, frameWidth, frameHeight);
			}
		
        animation = new Animation(frameDuration, frames);
        curFrame = animation.getKeyFrame(timer, true);
	}
	
	public void draw(SpriteBatch sb, float x, float y)
	{
        timer += Gdx.graphics.getDeltaTime();
        curFrame = animation.getKeyFrame(timer, true);
        sb.draw(curFrame, x, y);
	}
	
	public int getID()
	{
		return id;
	}
	
	public boolean isFinishOnce()
	{
		return timer > animation.frameDuration * frames.length;
	}
	
	public void getID(int id)
	{
		this.id = id;
	}
	
	public Vector2 getDimension()
	{
		if(curFrame!= null)
			return new Vector2(curFrame.getRegionWidth(), curFrame.getRegionHeight());
		return new Vector2(0,0);
	}
}
